﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using BLToolkit.Data;
using BLToolkit.Data.Linq;
using Entities.IRepository;

namespace DAL.Admin
{
    public class BLTQuanLiTaiKhoanRepository :ConnectionStringBL, IQuanLiTaiKhoanRepository
    {
       //private DbManager db = new DbManager("QuanLiBongDa");
 

        public bool ThemQLTaiKhoan(QuanLiTaiKhoan qltk)
        {
            try
            {
               int kq= db.Insert(qltk);
                db.CommitTransaction();
                return kq > 0;
            }
            catch (Exception)
            {

                db.RollbackTransaction();
                return false;
            }
        }

        public bool CapNhatQLTaiKhoan(QuanLiTaiKhoan qltk)
        {
            try
            {
                int kq = db.Update(qltk);
                db.CommitTransaction();
                return kq > 0;
            }
            catch (Exception)
            {

                db.RollbackTransaction();
                return false;
            }
        }

        public bool XoaQLTaiKhoan(QuanLiTaiKhoan qltk)
        {
            try
            {
                int kq = db.Delete(qltk);
                db.CommitTransaction();
                return kq > 0;
            }
            catch (Exception)
            {

                db.RollbackTransaction();
                return false;
            }
        }

        public QuanLiTaiKhoan LayTheoId(string idnv, int idgiadau)
        {
            return db.GetTable<QuanLiTaiKhoan>().Where(x => x.MaNhanVien == idnv)
                .Where(x => x.MaGiaiDau == idgiadau)
                                .SingleOrDefault();
        }

        public QuanLiTaiKhoan LayChiTietTheoId(string idnv, int idgiadau)
        {
            QuanLiTaiKhoan qltk = LayTheoId(idnv, idgiadau);
            if (qltk != null)
            {
                qltk.GiaiDaus = db.GetTable<GiaiDau>().
                    SingleOrDefault(x => x.MaGiaiDau == qltk.MaGiaiDau);
                qltk.TaiKhoans = db.GetTable<TaiKhoan>().
                   SingleOrDefault(x => x.MaTaiKhoan == qltk.MaTaiKhoan);
                qltk.NhanViens = db.GetTable<NhanVien>().
                   SingleOrDefault(x => x.MaNhanVien == qltk.MaNhanVien);
                
            }
            return qltk;
        }

        public List<QuanLiTaiKhoan> LayTatCa()
        {
           return db.GetTable<QuanLiTaiKhoan>().ToList();
        }
    }
}
